import pandas as pd
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
import re
import numpy as np
import os

#Run file in the code directory

cwd = os.getcwd()
path = cwd[:-5]

os.chdir(path)

fjc = pd.read_excel('Data/Raw/FJC_Judge_Data.xlsx')
jcs = pd.read_excel('Data/Raw/JCS_COA_Epstein_et_al.xlsx')

fjc_coa = []
for i in range(len(fjc)):
    c=0
    for j in range(1,7):
        s = 'Court Type ('+str(j)+')'
        if 'Appeals' in str(fjc[s][i]):
            c=1
            break
    fjc_coa.append(c)

fjc_middle = [fjc['Middle Name'][i] if str(fjc['Middle Name'][i])!='nan' else '' for i in range(len(fjc))]
fjc_name = fjc['Last Name']+', '+fjc['First Name'] +' '+fjc_middle
fjc['Name'] = fjc_name
fjc['COA'] = fjc_coa


matches = []
ratios = []
good_names = []
for name in jcs['name']:
    good_name = 'ERROR'
    nid = 'ERROR'
    s=0
    for i in range(len(fjc['Name'])):
        if fjc['COA'][i]==1:
            if name.split(',')[0]==fjc['Last Name'][i]:
                s1 = fuzz.partial_ratio(name,fjc['Name'][i])
                if s1> s:
                    s=s1
                    nid = fjc['nid'][i]
                    good_name = fjc['Name'][i]
    ratios.append(s)
    good_names.append(good_name)
    matches.append(nid)
jcs['nid']=matches 
jcs['ratio']=ratios
jcs['matched_name']=good_names


jcs.to_csv('Data/Raw/jcs_nid.csv',index=False)